import { createWebHistory, createRouter } from "vue-router";
import route from "./autoCreateRoutes";
import config from "@/configs/globalConfig";
import base from "./base";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/",
      name: "Home",
      component: () => import("@/components/Basic/Layout/index.vue"),
      children: [...route, ...base],
      meta: {
        title: "仪表盘",
      },
    },
    {
      path: "/login",
      name: "Login",
      component: () => import("@/views/Login/index.vue"),
    },
    {
      path: "/404",
      name: "404",
      component: () => import("@/views/404/index.vue"),
    },
    // 匹配所有以上不满足的页面
    {
      path: "/:pathMatch(.*)",
      redirect: "/404",
    },
  ],
});

router.beforeEach((to, from, next) => {
  // if (to.meta.isAuth) {
  //   // 表示需要鉴权
  //   const token = getToken(); // 获取 token 方式
  //   if (token) {
  //     // 存在 进一步判断token是否有效
  //     next();
  //   } else {
  //     // 不存在
  //     next("/login");
  //   }
  // } else if (to.path === "/login") {
  //   // 判断是否已经登录
  //   next();
  // } else {
  //   next();
  // }

  next();
  // 修改页面标题
  document.title =
    `${to.meta.title ? to.meta.title + " | " : ""}` + config.system.name;
});

export default router;
